﻿Public Class xtf_LISTADOcaja_

    Public modo As cls_caja_.enu_tipo

    Public Overrides Sub IniciarLista()
        Try
            gdc_general.BeginUpdate()
            gdc_general.DataSource = dtt_DataTable
            'ulg_Lista.DisplayLayout.GroupByBox.Style = Infragistics.Win.UltraWinGrid.GroupByBoxStyle.Full

            'Creacion y carga de Cuentas
            dtt_DataTable.Columns.Add("Identificador (ID)")
            dtt_DataTable.Columns.Add("Fecha", GetType(DateTime))
            dtt_DataTable.Columns.Add("Día")
            dtt_DataTable.Columns.Add("Saldo", GetType(Decimal))

            gdv_general.PopulateColumns(dtt_DataTable)

            gdv_general.Columns("Saldo").DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            gdv_general.Columns("Saldo").DisplayFormat.FormatString = "n" & int_cantdecimales
            gdv_general.Columns("Saldo").AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far
            gdv_general.Columns("Saldo").AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far

            Dim obj_Caja_ As New cls_caja_
            If obj_Caja_.CargarXtipo(modo, ObtenerRegistroEstado()) Then
                For Each Caja As cls_caja_ In obj_Caja_.Items
                    AgregarItem(Caja)
                Next
            End If
            'ulg_Lista.DataBind()

            FiltrarColumnas()
            gdc_general.EndUpdate()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Function AgregarItem(ByRef Caja As cls_caja_) As DataRow
        Dim Row As DataRow = dtt_DataTable.NewRow
        Row("Identificador (ID)") = Caja.idcaja
        Row("Fecha") = Caja.fecha.ToShortDateString
        Row("Día") = UCase(Format(Caja.fecha, "dddd"))
        Row("Saldo") = Caja.total

        dtt_DataTable.Rows.Add(Row)
        'ulg_Lista.Rows(ulg_Lista.Rows.Count - 1).Height = 19
        Return Row
    End Function

    Public Sub ModificarItem(ByRef Caja As cls_caja_, ByRef ulg_row As DataRow)
        ulg_row("Identificador (ID)") = Caja.idcaja
        ulg_row("Fecha") = Caja.fecha.Date.ToShortDateString
        ulg_row("Día") = UCase(Format(Caja.fecha, "dddd"))
        ulg_row("Saldo") = Caja.total
    End Sub

    Public Sub New(xtf_tipo As cls_caja_.enu_tipo)

        Me.Cursor = Cursors.WaitCursor
        InitializeComponent()
        modo = xtf_tipo

        Select Case modo
            Case cls_caja_.enu_tipo.Mostrador
                PERMISONuevo = "012001002"
                PERMISOModificar = "012001003"
                PERMISOEliminar = "012001003"
                PERMISOImprimir = "012001003"
                lbl_modulo.Text = "Caja Mostrador"
            Case cls_caja_.enu_tipo.Reparto
                PERMISONuevo = "012002002"
                PERMISOModificar = "012002003"
                PERMISOEliminar = "012002004"
                PERMISOImprimir = "012002005"
                lbl_modulo.Text = "Caja Reparto"
        End Select
        
        Me.Text = lbl_modulo.Text : lbl_modulodescripcion.Text = "Listado de " & lbl_modulo.Text & "."
        IniciarLista()
        Me.Cursor = Cursors.Default

    End Sub


    Public Overrides Sub bbi_Eliminar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOEliminar) Then
            Try
                If gdv_general.SelectedRowsCount > 0 Then
                    If ErrManagerShow("000005", Me) = DialogResult.Yes Then
                        Dim eliminarRegistro As Boolean = False
                        If UsuarioLOG.esroot Then
                            If ErrManagerShow("001023", Me) = DialogResult.Yes Then
                                eliminarRegistro = True
                            End If
                        End If


                        Dim objeto As New cls_caja_

                        If eliminarRegistro Then
                            objeto.Eliminar(gdv_general.GetDataRow(gdv_general.GetSelectedRows(0))("Identificador (ID)"))
                        Else
                            objeto.Buscar(gdv_general.GetDataRow(gdv_general.GetSelectedRows(0))("Identificador (ID)"))
                            objeto.registro.idregistroestado = 2
                            objeto.Modificar()
                            If Not IsNothing(objeto.cajaasiento_) Then
                                For Each cajaasiento In objeto.cajaasiento_.Items
                                    If cajaasiento.idasiento <> 0 Then
                                        Dim asiento As New cls_asiento_
                                        If asiento.Buscar(cajaasiento.idasiento) Then
                                            asiento.registro.idregistro = 2
                                            asiento.Modificar()
                                        End If
                                    End If
                                Next
                            End If
                        End If

                        dtt_DataTable.Rows.Remove(gdv_general.GetDataRow(gdv_general.GetSelectedRows(0)))
                    End If
                End If
            Catch ex As Exception
                'Dim frmError As New frm_ErrorManager(ex.Message)
            End Try
        End If

    End Sub

    Public Overrides Sub bbi_Nuevo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISONuevo) Then
            bsi_estado.Caption = "Cargando..."
            Application.DoEvents()
            Dim ABM As New xtf_ABMcaja(modo)
            ABM.Nuevo(Me)
            ABM.ControlarModificaciones(ABM.lac_general.items)
            bsi_estado.Caption = "Listo"
        End If
        Strings.Left(Replace(Me.Name, "xtf_LISTADO", "xtf_ABM"), Replace(Me.Name, "xtf_LISTADO", "xtf_ABM").Length - 2)
    End Sub

    Public Overrides Sub bbi_Modificar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        If ComprobarPermiso(PERMISOModificar) Then
            Try
                Dim barrio As Object 'New cls_barrio_
                barrio = Activator.CreateInstance(Type.GetType(Replace(Application.ProductName, " ", "_") & "." & Replace(Me.Name, "xtf_LISTADO", "cls_"), True, True))
                If gdv_general.SelectedRowsCount > 0 Then
                    If barrio.Buscar(gdv_general.GetDataRow(gdv_general.GetSelectedRows(0))("Identificador (ID)")) Then
                        bsi_estado.Caption = "Cargando..."
                        Application.DoEvents()
                        Dim ABM As New xtf_ABMcaja(modo)
                        ABM.Modificar(barrio, Me, gdv_general.GetDataRow(gdv_general.GetSelectedRows(0)))
                        ABM.ControlarModificaciones(ABM.lac_general.items)
                        bsi_estado.Caption = "Listo"
                    End If
                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    End Sub
End Class